package com.jasiri.project.app

import androidx.lifecycle.ProcessLifecycleOwner
import com.jasiri.project.BuildConfig
import com.jasiri.project.analysis.TestAnalysisInit
import com.jasiri.project.base.BaseApp
import com.jasiri.project.lib.location.JasiriLocationHelper
import com.jasiri.project.lib.sensor.JasiriSensorHelper
import com.jasiri.project.ui.permission.PermitUtil
import com.jasiri.project.utils.config.Configs
import com.jasiri.project.utils.event.track.TrackEventManager
import com.jasiri.project.utils.http.HttpHelper
import com.jasiri.project.utils.http.OkHttpLoaderV2
import com.jasiri.project.utils.log.HHLog
import com.jasiri.project.utils.sp.SPUtils
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty


/**
 * Sikadwa
 * @Author：wwdeng
 * @CreateTime：2024/4/14 19:27
 * <pre>
 *
 * </pre>
 */
class App: BaseApp() {

    companion object {
        private var instance: App by NotNullSingleValueVar()
        fun  instance() = instance
        //此字段用于记录应用前后台切换时埋点的remark，只用作记录进件信息
        var curtPageRemark = ""

        /**
         * 获取当前手机屏幕的宽
         */
        fun getPhoneWidth(): Int {
            // 手机屏幕的宽
            // 获取DisplayMetrics对象
            val displayMetrics = instance().resources.displayMetrics
            return displayMetrics.widthPixels
        }

        public fun getPhoneHeight(): Int {
            // 手机屏幕的高
            // 获取DisplayMetrics对象
            val displayMetrics = instance().resources.displayMetrics
            return displayMetrics.heightPixels
        }
    }

    override fun onCreate() {
        super.onCreate()
        instance = this

        initLibConfigs()
    }

    override fun onTerminate() {
        TrackEventManager.destroy()
        super.onTerminate()
    }

    /**
     * 初始化Lib
     */
    private fun initLibConfigs(){
        //应用前后台监听
        ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifecycleObserver())
        //Log日志
        HHLog.debug(BuildConfig.isDebug)
//        HttpHelper.init(OkHttpLoader())
        HttpHelper.init(OkHttpLoaderV2())
        //初始化读取所有权限
        PermitUtil.readPermitManager(SPUtils.getAppSp())
        //初始化 analysis
        TestAnalysisInit.initAnalysisLibAtAppLaunch()
        //初始化批量埋点工具
        TrackEventManager.init(this)
    }

    /**
     * 注册陀螺仪监听
     * 建议在获取权限之后注册
     */
    fun registerSensor(){
        //权限页操作状态 : 0未操作  1已同意  -1拒绝
        val perStatus = Configs.getSPInt(Configs.SPKEY.JASIRI_PERMISSION_REASON_STATUS)
        if (perStatus == 1){
            JasiriSensorHelper.register()
        }
    }

    //定义一个属性管理类，进行非空和重复赋值的判断
    private class  NotNullSingleValueVar<T>() : ReadWriteProperty<Any?, T> {
        private var value: T? =null
        //非空的校验
        override fun getValue(thisRef: Any?, property: KProperty<*>): T {
            return value ?: throw IllegalArgumentException("application not initialized")
        }
        //重复赋值的校验
        override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
            this.value=if (this.value==null) value
            else throw IllegalArgumentException("application already initialized")
        }
    }
}